﻿using Sunny.UI;
using sync_signal_light.subs;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace sync_signal_light
{
    public partial class FrmIndex : Sunny.UI.UIForm
    {
        public FrmIndex()
        {
            InitializeComponent();
        }
        private mdbs mdb;
        private void FrmIndex_Load(object sender, EventArgs e)
        {
            try
            {
                this.Text = "航空障碍灯控制软件 - " + Application.ProductVersion;
                opini.CLS_CFG = Json.Deserialize<cls_config>(opini.read_config("system", "configs"));
                if (opini.CLS_CFG == null)
                {
                    opini.CLS_CFG = new cls_config();
                    opini.write_config("system", "configs", Json.Serialize(opini.CLS_CFG));
                }

                //opini.CLS_PARA = Json.Deserialize<cls_parameters>(opini.read_config("system", "parameters"));
                //if (opini.CLS_PARA == null)
                //{
                //    opini.CLS_PARA = new cls_parameters();
                //    opini.write_config("system", "parameters", Json.Serialize(opini.CLS_PARA));
                //}
                //if (opini.CLS_PARA.auto)
                //{
                //    btn_start.Visible = false;
                //}
                btn_start.Visible = !opini.CLS_CFG.start_enable;
                opini.CLS_PARA.auto = opini.CLS_CFG.start_enable;

                //chk_sync.Visible = !opini.CLS_CFG.sync_enable;//隐藏同步控制
                //TIM_SYNC.Enabled = opini.CLS_CFG.sync_enable;//启用信号同步
                loading_devs();

                mdb = new mdbs();
                Task.Run(() => { mdb.LightCollectTask(); });
                Task.Run(() => { mdb.AlarmControlTask(); });

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void loading_devs()
        {
            PLEDEVS.Controls.Clear();
            opini.DicDevs = new Dictionary<int, cls_device>();
            foreach (string addr in opini.CLS_CFG.addrs)
            {
                subs.devs dev = new subs.devs();
                dev.Initing(addr);
                dev.Dock = DockStyle.Top;
                PLEDEVS.Controls.Add(dev);
                cls_device devc = new cls_device();
                if (opini.DicDevs.ContainsKey(int.Parse(addr)) == false) opini.DicDevs.Add(int.Parse(addr), devc);
                else MessageBox.Show("设备地址重复！");
            }
        }
        private void btn_event_click(object sender, EventArgs e)
        {
            UISymbolButton btn = sender as UISymbolButton;
            switch (btn.Name.ToLower())
            {
                case "btn_config":
                    {
                        FrmSetting frm = new FrmSetting();
                        frm.ShowDialog();
                    }
                    break;
                case "btn_start":
                    {
                        opini.CLS_PARA.auto = !opini.CLS_PARA.auto;
                        if (opini.CLS_PARA.auto)
                        {
                            btn_start.Text = "采集中..";
                            btn_start.Symbol = 61515;
                        }
                        else
                        {
                            btn_start.Text = "待采集";
                            btn_start.Symbol = 61516;
                        }
                    }
                    break;
                case "btn_sends":
                    {
                        send_event();
                    }
                    break;
                case "btn_logs":
                    {
                        FrmLogs frm = new FrmLogs();
                        frm.Show();
                    }
                    break;
                case "btn_exit":
                    {
                        Application.Exit();
                    }
                    break;
            }
        }
        private void send_event()
        {
            laoding_chks();//加载待写入列表
            foreach (devs ndevs in PLEDEVS.Controls)
            {
                if (ndevs.check_sate.Checked)
                {
                    byte addr = byte.Parse(ndevs.Address.Text);
                    foreach (var wrt in dicwrts.Values)
                    {
                        opini.CLS_PARA.cwt.Add(new cls_writes() { address = addr, reister = wrt.reister, value = wrt.value });
                    }
                }
            }
        }

        private int Nsec = 0;
        private void TIMS_Tick(object sender, EventArgs e)
        {
            btn_sends.Enabled = btn_logs.Enabled = opini.CLS_PARA.auto;
            if (opini.CLS_PARA.update)
            {
                loading_devs();
                opini.CLS_PARA.update = false;
            }
            if ((opini.AlarmCode > 0) || (opini.AlarmLose > 0))
            {
                AlarmFlag.SymbolColor = Color.Red;
                if (DateTime.Now.Second % 2 == 0)
                {
                    AlarmFlag.Symbol = 557424;
                }
                else
                {
                    AlarmFlag.Symbol = 557421;
                }
            }
            else
            {
                AlarmFlag.SymbolColor = Color.Blue;
                AlarmFlag.Symbol = 557423;
            }
        }
        private Dictionary<string, cls_writes> dicwrts = new Dictionary<string, cls_writes>();
        private void laoding_chks()
        {
            dicwrts = new Dictionary<string, cls_writes>();
            if (chk_light_level.Checked)
            {
                cls_writes wrt = new cls_writes();
                wrt.address = 0;
                wrt.reister = 1;
                wrt.value = int.Parse(light_level.Text);
                dicwrts.Add("light_level", wrt);
            }
            if (chk_day_enable.Checked)
            {
                cls_writes wrt = new cls_writes();
                wrt.address = 0;
                wrt.reister = 2;
                wrt.value = int.Parse(day_enable.Text);
                dicwrts.Add("day_enable", wrt);
            }
            if (chk_day_light.Checked)
            {
                cls_writes wrt = new cls_writes();
                wrt.address = 0;
                wrt.reister = 3;
                wrt.value = int.Parse(day_light.Text);
                dicwrts.Add("day_light", wrt);
            }
            if (chk_day_cycle.Checked)
            {
                cls_writes wrt = new cls_writes();
                wrt.address = 0;
                wrt.reister = 4;
                wrt.value = int.Parse(day_cycle.Text);
                dicwrts.Add("day_cycle", wrt);
            }
            if (chk_day_delay.Checked)
            {
                cls_writes wrt = new cls_writes();
                wrt.address = 0;
                wrt.reister = 5;
                wrt.value = int.Parse(day_delay.Text);
                dicwrts.Add("day_delay", wrt);
            }
            if (chk_night_enable.Checked)
            {
                cls_writes wrt = new cls_writes();
                wrt.address = 0;
                wrt.reister = 6;
                wrt.value = int.Parse(night_enable.Text);
                dicwrts.Add("night_enable", wrt);
            }
            if (chk_night_light.Checked)
            {
                cls_writes wrt = new cls_writes();
                wrt.address = 0;
                wrt.reister = 7;
                wrt.value = int.Parse(night_light.Text);
                dicwrts.Add("night_light", wrt);
            }
            if (chk_night_cycle.Checked)
            {
                cls_writes wrt = new cls_writes();
                wrt.address = 0;
                wrt.reister = 8;
                wrt.value = int.Parse(night_cycle.Text);
                dicwrts.Add("night_cycle", wrt);
            }
            if (chk_night_delay.Checked)
            {
                cls_writes wrt = new cls_writes();
                wrt.address = 0;
                wrt.reister = 9;
                wrt.value = int.Parse(night_delay.Text);
                dicwrts.Add("night_delay", wrt);
            }

        }

        private void FrmIndex_FormClosed(object sender, FormClosedEventArgs e)
        {
            mdb.ClosePorts();
            Application.Exit();
        }

        private void chk_sync_CheckedChanged(object sender, EventArgs e)
        {
            if (chk_sync.Checked)
            {
                chk_sync.Text = "启用信号同步";
            }
            else
            {
                chk_sync.Text = "禁用信号同步";
            }
            TIM_SYNC.Enabled = chk_sync.Checked;
        }
        private void TIM_SYNC_Tick(object sender, EventArgs e)
        {
            if (++Nsec >= opini.CLS_CFG.sync_delay)
            {
                Nsec = 0;
                opini.CLS_PARA.cwt.Add(new cls_writes() { address = 0, reister = 11, value = 1 });
            }
            chk_sync.Text = "启用信号同步 " + Nsec.ToString();
        }
    }
}
